import Vue from 'vue';

import VueRouter from "vue-router";
import Store from '@/store';

Vue.use(VueRouter);


//重写push |replace   解决了NavigationDuplicated报错问题
let  originPush=VueRouter.prototype.push;
let originReplace=VueRouter.prototype.replace;
/*
*
* 第一个参数：跳转的路径
* 第二个参数：成功的回调
* 第三个参数：失败的回调
* */
VueRouter.prototype.push=function (location,resolve,reject) {
    if (resolve&&reject){
        //call和apply的区别
        //相同点：都可以调用函数一次，都可以篡改函数上下文一次
        //不同点：call和apply传递参数：call传递的参数用逗号隔开，apply执行方法传递数组
        originPush.call(this,location,resolve,reject)
    }else {
        originPush.call(this,location,()=>{},()=>{})
    }
}
VueRouter.prototype.replace=function (location,resolve,reject) {
    if (resolve&&reject){
        originReplace.call(this,location,resolve,reject)
    }else {
        originReplace.call(this,location,()=>{},()=>{})
    }

}

const router = new VueRouter({
routes:[
    {
        path: '/test',
        name: 'test',
        component: () =>
            import('@/pages/Test/index'),
        meta: {

        }
    },
    {
        path: '/home',
        name: 'home',
        component: () =>
            import('@/pages/Home'),
        children: [
            {
                path: '/keepAcc',
                name: 'keepAcc',
                component: () =>
                    import('@/pages/KeepAccount'),
                meta: {

                }
            },
            {
                path: '/note',
                name: 'note',
                component: () =>
                    import('@/pages/Note'),
                meta: {

                }
            },
            {
                path: '/content',
                name: 'content',
                component: () =>
                    import('@/pages/Content'),
                meta: {

                }
            },
            {
                path: '/plan',
                name: 'plan',
                component: () =>
                    import('@/pages/Plan'),
                meta: {

                }
            },
            {
                path: '/hot',
                name: 'hot',
                component: () =>
                    import('@/pages/hot'),
                meta: {

                }
            },
        ]
    },
    {
        path: '/demo1',
        name: 'demo1',
        component: () =>
            import('@/pages/Example/jinjin'),
    },
    {
        path: '/demo2',
        name: 'demo2',
        component: () =>
            import('@/pages/Example/wenhui'),
    },
    {
        path: '/demo3',
        name: 'demo3',
        component: () =>
            import('@/pages/Example/yingpeng'),
    },

    // {
    //     path: '/demo4',
    //     name: 'demo4',
    //     component: () =>
    //         import('@/pages/Example/yuzhe'),
    // }

    //项目跑起来以后重定向到首页
    {
        path:"/editmd",
        name: 'editmd',
        component: () =>
            import("@/components/comEdit")
    },
    {
        path:"/login",
        name: 'login',
        meta: { noLogin: true },
        component: () =>
            import("@/pages/Login")
    },
    {
        path:"/register",
        name: 'register',
        meta: { noLogin: true },
        component: () =>
            import("@/pages/Register")
    },
    {
        path:"/articleDetail",
        name: 'articleDetail',
        component: () =>
            import("@/pages/Note/article")
    },
    {
        path:"*",
        redirect:"/home"
    }
]
});

router.beforeEach((to, from, next) => {
  const token = Store.state.auth.token;

  if (to.path !== 'login' && (token === 'null' || !token)) {
    if (to.meta.noLogin) {
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
});

export default router;

